<?php
	$this->title = "Agenda";
	$this->headTitle( $this->title );
?>
<script>
	function limparForm()
	{
		for ( i = 0; i <= document.form.length; i++ )
		{
			if ( document.form[ i ] != undefined )
			{
				if ( ( document.form[ i ].type != "button" ) && ( document.form[ i ].type != "submit" ) ) document.form[ i ].value = "";
			}
		}
	}
	
	function detalharTelefone( intCoPessoa )
	{
		if ( $( "#telefone_" + intCoPessoa ).is( ":visible" ) )
		{
			$( "#img_" + intCoPessoa ).attr( "src" , "<?php echo $this->baseUrl( "/image/icoMais.gif" );?>" );
			$( "#telefone_" + intCoPessoa ).fadeOut( "slow" );
		}
		else
		{
			$( "#img_" + intCoPessoa ).attr( "src" , "<?php echo $this->baseUrl( "/image/icoMenos.gif" );?>" );
			$( "#telefone_" + intCoPessoa ).fadeIn( "slow" );
		}
	}

	function excluir( strUrl )
	{
		$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"Sim": function(){
					location.href = strUrl;
				},
				"N\343o": function() {
					$( this ).dialog( "close" );
				}
			}
		});
	}

	function alteraStatus( intCoPessoa )
	{
		$.ajax({
			url			: "<?php echo $this->url( array( "controller" => "pessoa" , "action" => "alterar-status" ) );?>" + "/co_pessoa/" + intCoPessoa ,
			dataType	: "json" ,
			context		: document.body ,
			type		: "GET" ,
			beforeSend	: function()
			{
                $( "#img_status_" + intCoPessoa ).attr( "src" , "<?php echo $this->baseUrl( "/image/mini_loading.gif" );?>" );
            } ,
			success:function( jsonResult )
			{
				if ( jsonResult.st_status == "OK" )
				{
					if ( jsonResult.st_ativo == "S" ) $( "#img_status_" + jsonResult.co_pessoa ).attr( "src" , "<?php echo $this->baseUrl( "/image/status/verde.jpg" );?>" );
					else $( "#img_status_" + jsonResult.co_pessoa ).attr( "src" , "<?php echo $this->baseUrl( "/image/status/cinza.jpg" );?>" );
				}
				else $( "#img_status_" + jsonResult.co_pessoa ).attr( "src" , "<?php echo $this->baseUrl( "/image/ico_alerta.jpg" );?>" );
			}
		});
	}
</script>
<div id="subTitulo">
	PESQUISAR
</div>
<form id="form" name="form" method="POST" action="<?php echo $this->url( array( "controller" => "pessoa" , "action" => "index" ) );?>">
	<div id="cadastroPequeno">
		<div id="contemLabel">
			<label class="form">Nome:</label>
		</div>
		<div>
			<input type="text" id="txt_no_pessoa" name="txt_no_pessoa" value="<?php echo( ( ! Library::isNullEmpty( $this->arrDataForm[ "no_pessoa" ] ) ) ? $this->arrDataForm[ "no_pessoa" ] : "" );?>"/>
		</div>
	</div>
	<div id="cadastroPequeno">
		<div id="contemLabel">
			<label class="form">Sexo:</label>
		</div>
		<div>
			<select id="sel_co_sexo_gn" name="sel_co_sexo_gn">
				<option value="" >Selecione...</option>
				<?php foreach( $this->arrSexoGn as $arrInfoSexoGn ):?>
					<option value="<?php echo $arrInfoSexoGn[ "co_generica" ];?>" <?php echo ( ( $this->arrDataForm[ "co_sexo_gn" ] == $arrInfoSexoGn[ "co_generica" ] ) ? "selected" : "" );?>>
						<?php echo $arrInfoSexoGn[ "ds_generica" ];?>
					</option>
				<?php endforeach;?>
			</select>
		</div>
	</div>
	<div id="cadastroPequeno">
		<div id="contemLabel">
			<label class="form">Estado Civil:</label>
		</div>
		<div>
			<select id="sel_co_estado_civil_gn" name="sel_co_estado_civil_gn">
				<option value="" >Selecione...</option>
				<?php foreach( $this->arrEstadoCivilGn as $arrInfoEstadoCivilGn ):?>
					<option value="<?php echo $arrInfoEstadoCivilGn[ "co_generica" ];?>" <?php echo ( ( $this->arrDataForm[ "co_estado_civil_gn" ] == $arrInfoEstadoCivilGn[ "co_generica" ] ) ? "selected" : "" );?>>
						<?php echo $arrInfoEstadoCivilGn[ "ds_generica" ];?>
					</option>
				<?php endforeach;?>
			</select>
		</div>
	</div>
	<div id="cadastroPequeno">
		<div id="contemLabel">
			<label class="form">Categoria:</label>
		</div>
		<div>
			<select id="sel_co_categoria_gn" name="sel_co_categoria_gn">
				<option value="" >Selecione...</option>
				<?php foreach( $this->arrCategoriaGn as $arrInfoCategoriaGn ):?>
					<option value="<?php echo $arrInfoCategoriaGn[ "co_generica" ];?>" <?php echo ( ( $this->arrDataForm[ "co_categoria_gn" ] == $arrInfoCategoriaGn[ "co_generica" ] ) ? "selected" : "" );?>>
						<?php echo $arrInfoCategoriaGn[ "ds_generica" ];?>
					</option>
				<?php endforeach;?>
			</select>
		</div>
	</div>
	<div id="cadastroPequeno">
		<div id="contemLabel">
			<label class="form">Situa&ccedil;&atilde;o:</label>
		</div>
		<div>
			<select id="sel_st_ativo" name="sel_st_ativo">
				<option value="" >Selecione...</option>
				<option value="S" <?php echo ( ( $this->arrDataForm[ "st_ativo" ] == "S" ) ? "selected" : "" );?>>Ativo</option>
				<option value="N" <?php echo ( ( $this->arrDataForm[ "st_ativo" ] == "N" ) ? "selected" : "" );?>>Inativo</option>
			</select>
		</div>
	</div>
	<div id="botoesCadastro">
		<input type="button" value="Limpar" onclick="limparForm();">
		<input type="submit" value="Pesquisar">
	</div>
</form>
<p>
	<a href="<?php echo $this->url( array( "controller" => "pessoa" , "action" => "form" ) );?>">Adicionar nova pessoa</a>
</p>
<table>
	<tr>
		<th>Nome</th>
		<th>Sexo</th>
		<th>Estado Civil</th>
		<th>Categoria</th>
		<th>Inclus&atilde;o/Altera&ccedil;&atilde;o</th>
		<th>A&ccedil;&atilde;o</th>
	</tr>
	<?php foreach( $this->arrObjPaginator as $objPaginator ):?>
		<tr>
			<td>
				<img id="img_<?php echo $objPaginator[ "co_pessoa" ];?>" src="<?php echo $this->baseUrl( "/image/icoMais.gif" );?>" onclick="detalharTelefone( <?php echo $objPaginator[ "co_pessoa" ];?> );" style="cursor:pointer;">
				<?php echo $this->escape( $objPaginator[ "no_pessoa" ] );?>
			</td>
			<td><?php echo $objPaginator[ "ds_sexo" ];?></td>
			<td><?php echo $objPaginator[ "ds_estado_civil" ];?></td>
			<td><?php echo $objPaginator[ "ds_categoria" ];?></td>
			<td align="center"><?php echo Library::convertDateBase( $objPaginator[ "dt_insercao" ] , "d/m/Y" );?></td>
			<td>
				<img title="Situa&ccedil;&atilde;o"	src="<?php echo $this->baseUrl( ( $objPaginator[ "st_ativo" ] == "S" ) ? "/image/status/verde.jpg" : "/image/status/cinza.jpg" );?>" onclick="alteraStatus( '<?php echo $objPaginator[ "co_pessoa" ];?>' );" id="img_status_<?php echo $objPaginator[ "co_pessoa" ];?>" style="cursor:pointer;">
				<img title="Editar Pessoa"			src="<?php echo $this->baseUrl( "/image/icoEditar.gif" );?>"	onclick="location.href=	'<?php echo $this->url( array( "controller" => "pessoa"		, "action" => "form"	, "co_pessoa" => $objPaginator[ "co_pessoa" ] ) );?>'"		style="cursor:pointer;">
				<img title="Inserir Telefone"		src="<?php echo $this->baseUrl( "/image/icoTelefone.gif" );?>"	onclick="location.href=	'<?php echo $this->url( array( "controller" => "telefone"	, "action" => "form"	, "co_pessoa" => $objPaginator[ "co_pessoa" ] ) );?>'"		style="cursor:pointer;">
				<img title="Excluir Pessoa"			src="<?php echo $this->baseUrl( "/image/icoExcluir.gif" );?>"	onclick="excluir( '<?php echo $this->url( array( "controller" => "pessoa"			, "action" => "index"	, "co_pessoa" => $objPaginator[ "co_pessoa" ] ) );?>' );"	style="cursor:pointer;">
			</td>
		</tr>
		<?php if ( count( $objPaginator[ "ar_telefone" ] ) > 0 ):?>
			<tr>
				<td colspan="6">
					<table id="telefone_<?php echo $objPaginator[ "co_pessoa" ];?>" style="margin-left:30px; display:none;">
						<tr>
							<th>Tipo telefone</th>
							<th>Operadora</th>
							<th>Telefone</th>
							<th></th>
						</tr>
						<?php foreach( $objPaginator[ "ar_telefone" ] as $arrInfoTelefone ):?>
							<tr>
								<td><?php echo $arrInfoTelefone[ "ds_tipo_telefone" ];?></td>
								<td><?php echo $arrInfoTelefone[ "ds_operadora" ];?></td>
								<td><?php echo Library::formatTelefone( str_replace( " " , "" , $arrInfoTelefone[ "nu_telefone_completo" ] ) );?></td>
								<td>
									<img title="Editar Telefone"	src="<?php echo $this->baseUrl( "/image/icoEditar.gif" );?>"	onclick="location.href='<?php echo $this->url( array( "controller" => "telefone" , "action" => "form" , "co_telefone" => $arrInfoTelefone[ "co_telefone" ] ) );?>'" style="cursor:pointer;">
									<img title="Excluir Telefone"	src="<?php echo $this->baseUrl( "/image/icoExcluir.gif" );?>"	onclick="excluir( '<?php echo $this->url( array( "controller" => "telefone" , "action" => "excluir"	, "co_telefone" => $arrInfoTelefone[ "co_telefone" ] , "co_pessoa" => $objPaginator[ "co_pessoa" ] ) );?>' );" style="cursor:pointer;">
								</td>
							</tr>
						<?php endforeach;?>
					</table>
				</td>
			</tr>
		<?php else:?>
			<tr>
				<td colspan="6">
					<div id="telefone_<?php echo $objPaginator[ "co_pessoa" ];?>" style="margin-left:30px; display:none;">
						Nenhum registro a ser listado
					</div>
				</td>
			</tr>
		<?php endif;?>
	<?php endforeach; ?>
</table>
<div class="pagination">
    <?php echo $this->arrObjPaginator;?>
</div>
<div id="dialog-confirm" style="display:none;">
	<p>
		<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
		Deseja realmente excluir o registro?
	</p>
</div>
<?php if ( ! Library::isNullEmpty( $this->coPessoaTelefoneExcluido ) ):?>
	<script>
		detalharTelefone( <?php echo $this->coPessoaTelefoneExcluido; ?> );
	</script>
<?php endif;?>